#include "common.hpp"

void utils::binarizza(HERAuint8 byte,char *buff)
    {
        HERAuint8 probe=1;
        int i;
        //sprintf(buff,"");

        for(i=0;i<8;i++,probe<<=1)
            if(probe&byte) sprintf(buff,"%s1",buff);
            else sprintf(buff,"%s0",buff);
    }

void utils::bin_string(const HERAuint8 *buf, int size,char *str)
{
    int i;

        //sprintf(str,"");
    str[0]='\0';
        for(i=0; i<size; i++)
        {
            utils::binarizza(buf[i],str);
            sprintf(str,"%s ",str);
            switch(i)
            {
                case 4:
                case 9:
                case 15:
                case 21:
                case 27:
                case 33:
                case 39:sprintf(str,"%s\n",str); break;

            }
        }
    }

int  utils::hamming_dist(const HERAuint8 *b1,const HERAuint8 *b2,int len,int n_lastbits)
{
        int i,dist,j;
        HERAuint8 tmp,probe;
    //char str[255];

    //utils::bin_string(b1,len,str);
    //printf("%s\n",str);

    //utils::bin_string(b2,len,str);
    //printf("%s\n",str);

        dist = 0;
        for(i=0; i<len; i++)
        {
            tmp = b1[i] ^ b2[i];
            probe=1;
            if(i<len-1) {j=8;}
            else {j= n_lastbits;}

        //printf("probe = %X\n",probe);
        //utils::bin_string(&probe,1,str);
        //printf("%s\n",str);

            for(; j>0; j--)
            {
                if(tmp & probe) dist++;
                probe<<=1;
            }
        }

        return dist;
}
